package com.xqboss.apps.service.order.status.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xqboss.apps.domain.order.OrderMain;
import com.xqboss.apps.enums.order.OrderStatusEnum;
import com.xqboss.apps.mapper.order.OrderMainMapper;
import com.xqboss.apps.service.order.OrderService;
import com.xqboss.apps.service.order.status.OrderStatusExecutor;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author Trent
 * @date 2023/8/21
 */
@Component(value = OrderStatusEnum.Code.REFUND)
public class OrderRefundExecutor extends OrderStatusExecutor {

    @Autowired
    @Lazy
    private OrderService orderService;

    @Override
    public void preValidate(OrderMain order) {
        if(order.getStatus() == OrderStatusEnum.REFUND){
            throw new ServiceException("订单已经处于售后状态！");
        }
        if(order.getStatus() != OrderStatusEnum.PAYED
        && order.getStatus() != OrderStatusEnum.COMPLETED){
            throw new ServiceException("当前订单状态不支持退款！");
        }
    }

    @Override
    public void execUpdate(OrderMain order) {
        order.setStatus(OrderStatusEnum.REFUND);
        boolean update = orderService.update(new LambdaUpdateWrapper<OrderMain>()
                .set(OrderMain::getStatus, order.getStatus())
                .eq(OrderMain::getId, order.getId()));
        if(!update){
            throw new ServiceException("订单状态更新失败！");
        }
    }

    @Override
    public void postExec(OrderMain order) {

    }
}
